package com.gridmobi.xrtb.component.lucene;

import com.gridmobi.xrtb.component.lucene.analyzer.CommaAnalyzer;
import org.apache.lucene.analysis.Analyzer;
import org.apache.lucene.analysis.TokenStream;
import org.apache.lucene.analysis.standard.StandardAnalyzer;
import org.apache.lucene.analysis.tokenattributes.CharTermAttribute;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

/**
 * @author huangyongchao
 * 显示分词结果的工具类
 */
public class AnalyzerUtil {
    /**
     * @param text     带分词文本
     * @param analyzer 分词器
     * @return
     */
    public static List<String> analyzer(String text, Analyzer analyzer) {
        if (analyzer == null) {
            analyzer = new StandardAnalyzer();
        }
        List<String> response = new ArrayList<String>();
        TokenStream tokenStream = null;
        try {
            tokenStream = analyzer.tokenStream("content", new StringReader(text));
            CharTermAttribute attr = tokenStream.addAttribute(CharTermAttribute.class);
            tokenStream.reset();
            while (tokenStream.incrementToken()) {
                response.add(attr.toString());
            }
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (tokenStream != null) {
                try {
                    tokenStream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return response;
    }

    public static void main(String[] args) {
        analyzer("3, 4", new CommaAnalyzer());
    }
}
